<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>购物车</title>
    <link rel="stylesheet" href="/wechat/home/empty.css">
    <link rel="stylesheet" href="/wechat/home/cart.css">
    <jsp:include page="../public/head.jsp" flush="true"/>
    <style>
        .tip1 {
            margin-bottom: 20px;
            background: #fff;
            border: 1px solid transparent;
            border-radius: 10px;
            border: 1px solid #ddd;
            box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
            position: fixed;
            top: 50%;
            left: 50%;
            padding: 20px;
            min-width: 60%;
        }

        .tip1 span {
            word-break: normal;
            display: block;
            white-space: pre-wrap;
            word-wrap: break-word;
            overflow: hidden;
            text-align: center;
            font-size: 1.5em;
            color: #999999
        }

        .coupon_container {
            background: white;
            height: 3rem;
            line-height: 3rem;
            border-top: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            background: white;
        }

        .coupon_container:active {
            background: #eee !important;
        }

        .coupon_container .coupon_left {
            padding-left: 1rem;
        }

        .coupon_container .coupon_right {
            padding-right: 1rem;
            color: rgb(253, 161, 8);
        }

        .addressMsg {
            margin-top: 1rem;
        }

        .addressMsg input {
            padding: 1rem 1rem 1rem 0;
            border: 0;
            display: block;
            font-family: "microsoft yahei";
            color: black;
            font-size: 1.5rem;
        }

        .addressMsg #consignee {

        }

        .addressMsg #phone {

        }
    </style>
</head>
<body>

<header style="display:none;">
    <div class="h-cont">
        <div class="h-l"><i class="iconfont icon-person" onclick="history.go(-1)">&#xe625;</i></div>
        <div class="h-r"><span>${title}</span></div>
        <c:if test="${!empty sessionScope.shopCar}">
            <div class="h-t"><a onclick="cleanShopCar()"><i class="iconfont"
                                                            style="font-size: 30px">&#xe60b;</i></a>
            </div>
        </c:if>
    </div>
</header>
<div class="h-ocupie" style="display:none;"></div>

<section>
    <div class="my_cart">
        <c:choose>
            <c:when test="${empty sessionScope.shopCar}">
                <div class="my_empty">
                    <section>
                        <div class="pos_place"></div>
                        <div class="tit">购物车空空如也</div>
                        <a href="/home/beforelogin/index.do" class="gobuy">去购物</a>
                    </section>
                </div>
            </c:when>
            <c:otherwise>
                <c:choose>
                    <c:when test="${address == null}">
                        <!--无收货地址-->
                        <div class="addr_o">
                            <div class="a_c"><a
                                    href="/ucenter/addAddress.do?target=\/home\/shopCar.do"><i
                                    class="iconfont icon-me">&#xe60d;</i><span>请选择收收货地址</span></a>
                            </div>
                        </div>
                        <div class="border_b"></div>
                    </c:when>
                    <c:otherwise>
                        <form id="hForm" class="dForm">
                            <!--收货地址显示-->
                                <%--如果是酒店的话--%>
                            <c:if test="${ isHotel == null || isHotel==0}">
                                <a onclick=" window.location.replace('/ucenter/myAddress.do?choose=1')">
                                    <div class="addr_o">
                                        <div class="a_l"><i class="iconfont icon-me">&#xe609;</i>
                                        </div>
                                        <div  class="a_r">
                                            <div class="txt1"><span class="s1">收货人:</span><span
                                                    class="s2">${address.consignee}</span><span
                                                    class="s3">${address.phone}</span></div>

                                            <div class="clear"></div>
                                            <div class="txt2"><span
                                                    class="s1">收货地址：${address.address}</span>

                                            </div>
                                        </div>
                                    </div>
                                </a>
                                <div class="border_b"></div>
                                <input type="hidden" value="${address.mobile}" name="mobile">
                                <input type="hidden" value="${address.id}" name="address">
                            </c:if>
                            <c:if test="${isHotel==1}">
                                <a>
                                    <div class="addr_o">
                                        <div class="a_l"><i class="iconfont icon-me">&#xe609;</i>
                                        </div>
                                        <div id="my_width" class="a_r">
                                            <div class="txt1">
                                                <div class="addressMsg">

                                                    <input class="myinput" type="text"
                                                           id="hotel_consignee" name="consignee"
                                                           placeholder="收货人">

                                                    <input class="myinput" type="text"
                                                           id="hotel_mobile" name="mobile"
                                                           placeholder="手机号">
                                                    <input class="myinput" type="text"
                                                           id="hotel_floorNo" name="floorNo"
                                                           placeholder="房间号">
                                                </div>
                                            </div>

                                            <div class="clear"></div>
                                            <div class="txt2"><span
                                                    class="s1">收货地址：${address.address}</span>
                                                <c:if test="${hotelNo=='didi'}">
                                                    <img style="margin-left: 1rem;height: 1.5rem" src="/wechat/images/activity.png">
                                                </c:if>
                                            </div>
                                        </div>
                                    </div>
                                </a>
                                <div class="border_b"></div>
                                <input type="hidden" value="" id="hotel_address" name="address">
                            </c:if>

                            <c:if test="${ isHotel == null || isHotel==0}">
                            <%--如果--%>
                            <div class="timer">
                                <div class="tip"> 送达时间:</div>
                                <select name="time">
                                    <option>08:15~08:30</option>
                                    <option>08:30~08:45</option>
                                    <option>08:45~09:00</option>
                                    <option>09:00~09:15</option>
                                    <option>09:15~09:30</option>
                                </select>
                            </div>
                            </c:if>
                            <c:if test="${isHotel==1}">
                                <input  type="hidden"  name="time" value="08:00~08:15">
                                <p id="my_text1" style="padding:1rem;margin-bottom: 0;background: white">您的早餐将会在8点送达哦!</p>
                            </c:if>

                            <div class="coupon_container" onclick="gotoCoupon()">
                                <div class="coupon_left">优惠券</div>
                                <div class="coupon_right">选择我的优惠券</div>
                            </div>
                        </form>
                    </c:otherwise>
                </c:choose>
                <div class="status_list">
                    <c:if test="${hotelNo=='didi'}">
                        <div style="background: white;padding: 1rem;color: rgb(104,104,104);font-size: 1.6rem;">2017-03-08</div>
                    </c:if>
                    <c:forEach items="${sessionScope.shopCar}" var="item" varStatus="istatus">
                        <div id="${item.key}">
                            <div  class="tip_o my_tip_3">
                                <div class="tip"> ${item.key}</div>
                            </div>
                            <c:forEach items="${item.value}" var="value" varStatus="status">
                                <form id="${item.key}${status.index}" onsubmit="return false">
                                    <div class="goodscont">
                                        <div class="goods_pic"><a href="#"><img
                                                src="${value.goodNormalImage}"></a></div>
                                        <div class="goods_name">
                                            <div class="txt1 my_goodName">${value.goodName}</div>
                                                <%--<div class="txt1">05-09（周一）</div>--%>
                                            <div class="txt1 red">￥${value.goodPrcie}</div>
                                        </div>
                                        <div class="count">
                                            <span class="s1"
                                                  onclick="countSub('${istatus.index}','${status.index}','${item.key}')">-</span>
                                            <span class="s2"
                                                  id="${istatus.index}sc${status.index}">${value.goodAccount}</span>
                                            <span class="s3"
                                                  onclick="countAdd('${istatus.index}','${status.index}')">+</span>
                                        </div>
                                    </div>
                                    <div>
                                        <input type="hidden" value="${value.productClassId}"
                                               name="productClassId"/>
                                        <input type="hidden" value="${value.goodPrcie}"
                                               name="goodPrcie"
                                               id="${istatus.index}goodPrcie${status.index}">
                                        <input type="hidden" value="${value.categoryId}"
                                               id="categoryId">
                                        <input type="hidden" value="${value.goodId}" name="goodId">
                                        <input type="hidden" value="${value.shopId}" name="shopId">
                                        <input type="hidden" value="${value.goodNormalImage}"
                                               name="goodNormalImage">
                                        <input type="hidden" value="${value.goodName}"
                                               name="goodName">
                                        <input type="hidden" value="${value.goodAccount}"
                                               name="goodAccount"
                                               id="${istatus.index}c${status.index}">
                                    </div>
                                </form>
                            </c:forEach>
                        </div>
                    </c:forEach>


                    <div class="statusing">正在加载...</div>
                    <form id="fForm" class="dForm">
                        <input type="hidden" value="${totalSC.totalCount}" name="totalCount"
                               id="totalCount">
                        <input type="hidden" value="${totalSC.totalPrice}" name="totalPrice"
                               id="totalPrice">

                        <div id="my_class1" class="tip_o">
                            <c:if test="${hotelNo=='didi'}">
                                <div class="my_tip">温馨提示！请女王注意保持车内环境整洁！  <img style="height: 1.5rem" src="/wechat/images/prompt.png"></div>

                            </c:if>
                            <div class="tip3"> 订单备注：</div>
                            <input class="my_tip1" type="text" name="mark" placeholder="选填"></div>
                        <div class="info last">
                            <div class="txt1"><i class="iconfont icon-me">&#xe61f;</i>
                                <div class="num" id="count">${totalSC.totalCount}</div>
                            </div>
                            <div class="opra"><input type="button" name="" class="btn3" value="去结算"
                                                     onclick="buy()"></div>
                            <div class="txt2"><span class="s1 zongji">总计:</span><span
                                    class="s2 rmb">￥</span><span class="s2"
                                                             id="total">${totalSC.totalPrice}</span>
                            </div>
                        </div>
                    </form>
                </div>

            </c:otherwise>
        </c:choose>
    </div>

    </div>
</section>

<footer id="my_footer">
    <div class="my_ocupie"></div>
</footer>


<script>
    var hotelNo = '${hotelNo}';
    if(hotelNo=="didi"){
        $("#hotel_floorNo").css("display","none");
        $("#hotel_floorNo").val("38女王");
        $("#my_text1").css("display","none");
        $(".tip").css("display","none");
        $(".timer").append("2017-03-08");
        $("#my_class1").css("height","auto");
        $(".coupon_container").css("display","none");
        $(".goodscont").css({"background":"white","margin-top":"0.1rem"});
        $(".statusing").css("display","none");
        $(".tip3").text("女王陛下,您还满意？")
        $(".tip3").css({"padding":"1rem 1rem 0 1rem","font-size":"1.6rem","color":"rgb(104,104,104)"});
        $(".my_tip1").attr('placeholder','有什么意见或建议,请吩咐...');
        $(".my_tip1").css({"height":"auto","width":"100%","line-height":"auto","border-bottom":"none","padding":"0 1rem","margin-top":"0","font-size":"1.4rem","color":"rgb(104,104,104)"});
        $(".count").css("display","none");
        $(".my_tip").css({"font-size":"1.6rem","padding":"1rem","border-top":"1rem solid #eee","border-bottom":"1rem solid #eee","color":"rgb(104,104,104)"});
        $(".zongji").css({"color":"orange","max-width":"8rem"});
        $(".zongji").text("活动优惠价:");
        $(".rmb").css({"color":"orange"});
        $("#total").css({"color":"orange"});
        $("#count").css({"background":"orange","font-size":"1.5rem"});
        $(".txt1.red").css({"color":"orange"});
        $(".my_goodName").css({"color":"rgb(104,104,104)"});
        $("#hotel_mobile").css({"font-size":"1.7rem"})
        $("#hotel_consignee").css({"font-size":"1.7rem"})
        $(".my_tip_3").css({"display":"none"});
        $("#my_width").css("width","100%");
    }
    var flag = true ;

    var Couponname = '${CouponName}';
    if (Couponname != null && Couponname != '' && Couponname != 'undefined') {
        if (Couponname != 0) {
            $(".coupon_right").text(Couponname);
        }

    }


    settotalPrice();

    function gotoCoupon() {
        window.location.replace("/ucenter/myCoupon.do?totalPrice=${totalSC.totalPrice}");
    }

    function settotalPrice() {

        var lastvalue = $('#total').text();
        lastvalue = parseFloat(lastvalue).toFixed(2);
        $('#total').text(lastvalue);

    }


    function cleanShopCar() {
        if (confirm("是否清空购物车")) {
            $.get("/home/cleanShopCar.do", function (data) {
                if (!data.success) {
                    tip("", data.msg, true);
                } else {
                    window.location.reload();
                }
            }, "json");
        }
    }

    function countAdd(key, id) {
        var current = $('#' + key + 'c' + id).val();
        var currentCount = $("#count").text();
        var currenttotal = $("#total").text();
        var price = $('#' + key + 'goodPrcie' + id).val();
        current++;
        currentCount++;
        currenttotal = (parseFloat(price) + parseFloat(currenttotal)).toFixed(1);
        $('#' + key + 'c' + id).val(current);
        $('#' + key + 'sc' + id).text(current);
        $("#count").text(currentCount);
        $("#total").text(currenttotal);
        $("#totalCount").val(currentCount);
        $("#totalPrice").val(currenttotal);

    }
    function countSub(key, id, what) {
        var current = $('#' + key + 'c' + id).val();
        var currentCount = $("#count").text();
        var currenttotal = $("#total").text();
        var price = $('#' + key + 'goodPrcie' + id).val();
        if (current - 1 == 0) {
            if (confirm("是否删除")) {
                $.post("/home/deleteShopCar.do", {key: what, index: id}, function (data) {
                    if (!data.success) {
                        tip("", data.msg, true);
                    } else {
                        window.location.replace("/home/shopCar.do");
                    }
                }, "json");
            }
            return;
        }
        current--;
        currentCount--;
        currenttotal = (parseFloat(currenttotal) - parseFloat(price)).toFixed(1);
        $('#' + key + 'c' + id).val(current);
        $('#' + key + 'sc' + id).text(current);
        $("#count").text(currentCount);
        $("#total").text(currenttotal);
        $("#totalCount").val(currentCount);
        $("#totalPrice").val(currenttotal);
    }

    function buy() {

        if (!flag){
            return ;
        }
        flag =false;



        //如果是酒店的话
        if (${isHotel==1}) {
            //判断收货人或者手机号是否为空
            if ($("#hotel_consignee").val() == "") {
                alert("请输入收货人信息");
                return;
            }
            if ($("#hotel_mobile").val() == "") {
                alert("请输入手机号");
                return;
            }
            $.ajax({
                url: "/mobile/user/address",
                type: "POST",
                data: {
                    action: "add",
                    accountId: "${userinfo.id}",
                    mobile: "${userinfo.mobile}",
                    consignee: $("#hotel_consignee").val(),
                    cityId: "${address.cityId}",
                    cityName: "${address.cityName}",
                    areaId: "${address.areaId}",
                    areaName: "${address.areaName}",
                    builderId: "${address.builderId}",
                    builderName: "${address.builderName}",
                    detail: "",
                    address: "${address.address}",
                    phone: $("#hotel_mobile").val(),
                    floorNo: $("#hotel_floorNo").val(),
                },
                success: function (result) {

                    result = JSON.parse(result);
                    if (result.success == false) {
                        alert(result.msg);
                        flag=true;
                    } else {
                        $.get("/mobile/user/address?action=recentAddress&userId=${userinfo.id}", function (data) {
                            data = JSON.parse(data);


                            $("#hotel_address").val(data.data.id);
                            var user_address_id = data.data.id;
                            if ($("#hotel_address").val() == "") {
                                alert("地址出错");
                                return;
                            }

                            var keys = $("div[id^='201']");
                            var shopData = {};
                            $.each(keys, function (n, value) {
                                var goods = [];
                                var dates = $("form[id^='" + value.id + "']");
                                $.each(dates, function (j, data) {
                                    var form = $("#" + data.id);
                                    var o = form.serializeObject();
                                    goods.push(o);
                                });

                                shopData['0,' + value.id] = goods;
                            });


                            var data = $(".dForm").serializeObject();
                            data.shopData = shopData;
                            data.date = "周套餐日期";
                            data.signName = "测试";
                            data.userId = ${userinfo.id};

                            data.mark="酒店";
                            var CouponUserid = ${CouponUserId}+'';
                            var CouponId = ${CouponId}+'';

                            if (CouponUserid != null && CouponUserid != '' && CouponUserid != 'undefined') {
                                if (CouponId != null && CouponId != '' && CouponId != 'undefined') {

                                    data.couponUserId = CouponUserid
                                    data.couponId = CouponId
                                }
                                if (CouponUserid == 0 || CouponId == 0) {
                                    data.couponUserId = null;
                                    data.couponId = null;
                                }
                            }



                            $.post("/mobile/user/order?action=packages", {orderInfo: JSON.stringify(data)}, function (data) {
                                if (!data.success) {
                                    tip("", data.msg, true);
                                    flag=true;
                                } else {


                                    HotelOrderinc('${hotelInfo.hotelNo}', data.data.orderNo,user_address_id);

                                    <c:choose>
                                    <c:when test="${empty sessionScope.wxpay}">
                                    var http = "https://open.weixin.qq.com/connect/oauth2/authorize?";
                                    var appid = "&appid=wxf946190506655c2d";
                                    var redirect_uri = "&redirect_uri=" + encodeURI("http://m.52letsgo.cn/order/orderPay.do");
                                    var response_type = "&response_type=code";
                                    var scope = "&scope=snsapi_base";
                                    var state = "&state=" + data.data.orderNo;
                                    window.location.replace(http + appid + redirect_uri + response_type + scope + state + "#wechat_redirect");
                                    </c:when>
                                    <c:otherwise>
                                    window.location.replace("/order/orderPay.do?state=" + data.data.orderNo);
                                    </c:otherwise>
                                    </c:choose>


                                }
                            }, "json");


                        })

                    }
//
                }
            });


        } else {


            var keys = $("div[id^='201']");
            var shopData = {};
            $.each(keys, function (n, value) {
                var goods = [];
                var dates = $("form[id^='" + value.id + "']");
                $.each(dates, function (j, data) {
                    var form = $("#" + data.id);
                    var o = form.serializeObject();
                    goods.push(o);
                });

                shopData['0,' + value.id] = goods;
            });


            var data = $(".dForm").serializeObject();
            data.shopData = shopData;
            data.date = "周套餐日期";
            data.signName = "测试";
            data.userId = ${userinfo.id};
            var CouponUserid = ${CouponUserId}+'';
            var CouponId = ${CouponId}+'';

            if (CouponUserid != null && CouponUserid != '' && CouponUserid != 'undefined') {
                if (CouponId != null && CouponId != '' && CouponId != 'undefined') {

                    data.couponUserId = CouponUserid
                    data.couponId = CouponId
                }
                if (CouponUserid == 0 || CouponId == 0) {
                    data.couponUserId = null;
                    data.couponId = null;
                }
            }


            $.post("/mobile/user/order?action=packages", {orderInfo: JSON.stringify(data)}, function (data) {
                if (!data.success) {
                    tip("", data.msg, true);
                    flag=true;
                } else {
                    <c:choose>
                    <c:when test="${empty sessionScope.wxpay}">
                    var http = "https://open.weixin.qq.com/connect/oauth2/authorize?";
                    var appid = "&appid=wxf946190506655c2d";
                    var redirect_uri = "&redirect_uri=" + encodeURI("http://m.52letsgo.cn/order/orderPay.do");
                    var response_type = "&response_type=code";
                    var scope = "&scope=snsapi_base";
                    var state = "&state=" + data.data.orderNo;
                    window.location.replace(http + appid + redirect_uri + response_type + scope + state + "#wechat_redirect");
                    </c:when>
                    <c:otherwise>
                    window.location.replace("/order/orderPay.do?state=" + data.data.orderNo);
                    </c:otherwise>
                    </c:choose>
                }
            }, "json");
        }
    }


    function Map() {
        this.container = new Object();
    }


    Map.prototype.put = function (key, value) {
        this.container[key] = value;
    };


    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    function HotelOrderinc(hotelNo, orderNo,user_address_id) {
        //顺便删除地址
        $.ajax({
                    url: "/home/hotelOrderInc.do",
                    type: 'post',
                    data: {hotelNo: hotelNo, orderNo: orderNo, mobile: $("#hotel_mobile").val(),user_address_id:user_address_id},
                    success: function (data) {
                        console.log(data);
                    }
                }
        )
    }

</script>
</body>
</html>
